﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LWrap.IndexSource;
namespace LWrap.Index
{
    public class IndexWriter:IDisposable
    {
        private string _indexPath;
        private bool _isCreateNew;

        Lucene.Net.Store.Directory _directory;
        Lucene.Net.Analysis.Analyzer _analyzer;
        Lucene.Net.Index.IndexWriter _indexWriter;

        public bool IsIndexModificationInProgress
        {
            get;
            set;
        }
        public string IndexFilePath
        {
            get { return _indexPath; }
            set { _indexPath = value; }
        }

        public bool IsCreateNewIndex
        {
            get { return _isCreateNew; }
            set { _isCreateNew = value; }
        }

        public IndexWriter() { }
        public IndexWriter(string indexPath, bool isCreateNew)
        {
            _indexPath = indexPath;
            _isCreateNew = isCreateNew;
        }

        private void Initialize()
        {
            if (_indexWriter != null)
                this.Dispose();

            _directory = Lucene.Net.Store.FSDirectory.GetDirectory(_indexPath, _isCreateNew);
            _analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();

            //create the index writer with the directory and analyzer defined.
            _indexWriter = new Lucene.Net.Index.IndexWriter(_directory, _analyzer,/*true to create a new index*/ _isCreateNew);
            _indexWriter.SetMaxMergeDocs(100);
            //_indexWriter.SetMaxBufferedDocs(5);
        }
        internal void Execute(IndexDataSource indexSource)
        {
            this.IsIndexModificationInProgress = true;
            this.Initialize();
            this.IndexToFile(indexSource);
            this.Dispose();
            this.IsIndexModificationInProgress = false;
        }

        private int IndexToFile(IndexDataSource source)
        {
            IndexData indexData = new IndexData();
            while (source.GetNextDoc())
            {
                Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
                doc = indexData.PrepareField(doc, source);
                _indexWriter.AddDocument(doc);
            }
            return 1;
        }

        internal void DeleteDocument(string booleanCondition)
        {
            this.IsIndexModificationInProgress = true;
            if (_indexWriter == null)
                this.Initialize();

            LWrap.Search.SearchQueryParser queryMaker = new LWrap.Search.SearchQueryParser();
            Lucene.Net.Search.Query query = queryMaker.GetQuery(booleanCondition);
            _indexWriter.DeleteDocuments(query);

            this.Dispose();
            this.IsIndexModificationInProgress = false;
        }

        public void Dispose()
        {
            _analyzer.Close();
            _indexWriter.Close();
            _directory.Close();
            _indexWriter = null;
        }
    }
}
